/**
 * @fileOverview This file contains definition of HistogramWidget class.
 * @author GUI group 
 * @version 0.9 beta
 */
 

/**
* @class displays an histogram with a variabile number of bars for a single x value
* @param {String} id the id of the chart container. Must be unique inside the HTML document
* @param {int} width the width of the chart
* @param {int} height the height of the chart
* @augments BaseWidget
*/
function HistogramWidget(id, width, height){
	this.id = id;
	this.xtype = "auto"; // auto | int | date | string
	this.param = new Param();
	this.ypsilons = new Array();
	this.icses = null;
	this.min = null;
	this.max = null;
	this.d_out = null;
	this.d_in = null;
	this.zetas= new Array();
	this.param.w = width;
	this.param.h = height;
	
}
extend(HistogramWidget, BaseWidget);

/**
* @description creates the array containing the labels, formetted for the jqplot plugin
* @param {Array} array the array from which the labels must be extracted
* @private
*/
HistogramWidget.prototype.createLabelArray = function(array){
	var vett = new Array();
	for (var x in array){
		var asd = {};
		asd.label = array[x];
		vett.push(asd);
	}
	return vett;
}

/** 
* Gets an array of numerical values and display them in a histogram.
* The histogram will contains how many labels as how many are values in passed array 
* <br />If only values array is passed, the labels will be progressive numbers from 1 to the "values" array length
* <br />If only values array and dataout is passed, the labels will be strings representing the the day specified as dataout and the six
*  days before. If values array length is not equal 7 method will return -1;
* <br />If all parameters is passed and datain falls before dataout , the labels will be strings representing the days in the range [datain,dataout]. 
* If values array length is not equal the number of days in the range method will return -1;
* @param {Array} values the array of Y values for every element on the X axis
* @param {Date} dateout the last day in the range days
* @param {Date} datein  the first day in the range days
* @returns {Integer} 0 if parameter are valid -1 otherwise
*/
HistogramWidget.prototype.setDataArray = function(values,dataout,datain){
	if(values instanceof Array){
		
		this.max = Math.max.apply(Math, values);
		this.min = Math.min.apply(Math, values);
		if(this.min > 0) this.min = 0;

		if(arguments.length == 2 && dataout instanceof Date) {
			this.d_out = dataout;
			var lab_array = new Array();
			for(var h=(values.length-1); h > -1; h--){
				lab_array[h] = (zerofill(dataout.getDate(),2))+"/"+zerofill((dataout.getMonth()+1),2)+"/"+dataout.getFullYear();
				dataout.setDate(dataout.getDate()-1); 
			}
			this.setX(lab_array);
			this.setY("data1",values);
			
			return 0;
			
		}else if (arguments.length == 3 && dataout instanceof Date && datain instanceof Date && checkInterval(values.length,datain,dataout)) {
			this.d_out = dataout;
			this.d_in = datain;
			var lab_array = new Array();
			
			for(var h=(values.length-1); h > -1; h--){
				lab_array[h] = zerofill(dataout.getDate(),2)+"/"+zerofill((dataout.getMonth()+1),2)+"/"+dataout.getFullYear();
				dataout.setDate(dataout.getDate()-1); 
			}			
			this.setX(lab_array);
			this.setY("data2",values);
			
			return 0;
			
		}else{
			var lab_array = new Array();
			var h=0;
			while(h < values.length){
				lab_array[h] = ++h; 
			}
			this.setX(lab_array);
			this.setY("data1",values);
			return 0;
		}
	}
	
	return -1;
}



/**
* @description sets the values to be shown on the X axis. Consecutive invocations of this method overwrite the value every time.
* @param {Array} values the values to be set for the X axis
*/
HistogramWidget.prototype.setX = function(values){
	this.icses = values;
}

/**
* @description adds a new set of bars to the chart.
* @param {String} z the label of the new set of values
* @param {Array} values the array of Y values for every element on the X axis
*/
HistogramWidget.prototype.setY = function(z, values){
	this.ypsilons.push(values);
	this.zetas.push(z);
}

/**
* @description sets the type of data displayed on the X axis
* @param {String} t &quot;auto&quot; for auto-increment integer, &quot;int&quot; for an arbitrary set of integers,
* &quot;string&quot; for text strings, &quot;date&quot; for dates
* @ignore
*/
HistogramWidget.prototype.setXtype = function(t){
	if($.inArray(t, ["auto", "int", "string", "date"]) >= 0){
		this.xtype = t;
	}
}

/**
* @description Sets the chart title
* @param {String} title
*/
HistogramWidget.prototype.setTitle = function(title){
	if(typeof title == 'string')
		this.title = title;
}

/**
* @description creates the DOM element for the chart and displays it. Overwrites {@link BaseWidget#render}
* @param {String} div the ID of the element into which the chart must be displayed
*/
HistogramWidget.prototype.render = function(div){
	$("#" + div).append('<div id="'+this.id+'" style="position:relative; width:' + this.param.w +'px; height:' + this.param.h + 'px;"></div>');
	var zeta = this.createLabelArray(this.zetas);
	
	$.jqplot(this.id, this.ypsilons, {
		legend: {show: false, location: 'nw', xoffset: 30 },
		title: this.title || this.id,
		seriesDefaults:{
			renderer:$.jqplot.BarRenderer,
			rendererOptions:{barPadding: 8, barMargin: 20}
		},
		series:zeta,
		axes:{
			xaxis:{
				renderer:$.jqplot.CategoryAxisRenderer,
				ticks:this.icses
			},
			yaxis:{
				min: this.min,
	         	max: this.max	
			}
		}
	});
}


/** @ignore */
function zerofill(num,zeri){	
	var mynum = new String(num);

	while ( mynum.length < zeri) {
		mynum = "0" + mynum;
	}
	return mynum;
}
	
/** @ignore */
function checkInterval(expected,d1,d2){	
	
	if(d1>=d2){
		return false;
	}
	
	var i=0;
	while(d1<d2 && i<expected){
		d1.setDate(d1.getDate()+1);
		i++;
	}
	
	if(i == expected)
		return true;
	else
		return false;
	
}	